var work={
    ddressNum:1,//地址栏个数
    dataAll:"",//总数据
    strAll:[],//初始select数据
    addressDivId:1,//当前操作地址uid
    addressCount:1,//当前地址数
    addressIndex:'',//弹窗索引
    /*·····························初始化························*/
    init:function(){
        this.eventListen();
        $("#tel").val(info.tel);
        this.getCommunityData();
        this.getSelect();
        // this.formsub();
    },
    /*·····························DOM························*/
    eventListen:function(){


        // 地址栏目添加

        // 地址编辑


        //    第一级
        $(".xiaoqu").change(function () {

            //如果下一级元素有大于2个option那么就重置后续select
            if($(".quyu").children('option').length>1){
                $(this).nextAll().each(function(){
                    $(this).empty();
                });
                for(var i=0;i<work.strAll.length;i++){
                    $(this).nextAll().eq(i).html(work.strAll[i]);
                }
            }

            info.xiaoquId = $(this).val();
            console.log(info.xiaoquId);
            if(work.strLength(work.dataAll[info.xiaoquId].data)>0){
                var zones=work.dataAll[info.xiaoquId].data;
                console.log(zones);
                work.strAdd(zones,'quyu','zone');
            }else{
                work.msg('quyu','区域');
                var build=work.dataAll[info.xiaoquId][0].data;
                work.strAdd(build,'louhao');
            }

        });
        //    第二级
        $(".quyu").change(function(){
            info.quyuId = $(this).val();
            var build=work.dataAll[info.xiaoquId]['data'][info.quyuId].data;
            work.strAdd(build,'louhao','building');
        });
        //    第三级
        $(".louhao").change(function(){
            info.louhaoId = $(this).val();
            var unit=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId].data;
            console.log(unit);
            work.strAdd(unit,'danyuan','unit');
            for(x in unit){
                if(unit[x]['unit_id']==0){
                    $(".danyuan").find('option').eq(1).text('无单元');
                    var room=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId]['data'][0].data;
                    work.strAdd(room,'fangjian','room');
                }
            }
        });
        //    第四级
        $(".danyuan").change(function(){
            info.danyuanId = $(this).val();
            if(info.danyuanId){
                var room=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId]['data'][info.danyuanId].data;
                work.strAdd(room,'fangjian','room');
            }
        });
        // 确定按钮
        $(document).on("click",".addressCon",function(){
            var customerId=$(".add-"+work.addressDivId).find(".customerId").val();
            var familyId=$(".add-"+work.addressDivId).find(".familyId").val();
            var communityId=$(".xiaoqu").find("option:selected").val();
            var communityName=$(".xiaoqu").find("option:selected").text();
            var zoneId=$(".quyu").find("option:selected").val();
            var zoneName=$(".quyu").find("option:selected").text();
            var buildId=$(".louhao").find("option:selected").val();
            var buildName=$(".louhao").find("option:selected").text();
            var unitId=$(".danyuan").find("option:selected").val();
            var unitName=$(".danyuan").find("option:selected").text();
            var roomId=$(".fangjian").find("option:selected").val();
            var roomName=$(".fangjian").find("option:selected").text();
            work.addressPost(communityId,customerId,zoneId,unitId,buildId,roomId,familyId);
            work.addressShow(communityName,zoneName,buildName,unitName,roomName);
        });

    },

    // 地址确定显示中文
    addressShow:function(communityName,zoneName,buildName,unitName,roomName){
        var str=communityName+zoneName+buildName+unitName+roomName;
        $(".add-"+work.addressDivId).find(".addressInput").val(str);
    },
    // 五级联动
    //获取数据并设置小区下拉
    getCommunityData:function(){
        var idd = $("#idd").val();
        $.get("/index.php?m=Admin&c=House&a=jsonId&id="+idd,function(data){
            // var data=$.parseJSON(data);
            work.dataAll=data.data;
            data=data.data;
            // console.log(data);
            var str="";
            for(x in data){
                console.log(data[x]['community_name']);
                if(data[x]['community_name']){

                    str+='<option value="'+data[x]['community_id']+'">'+data[x]['community_name']+'</option>';
                }

            }
            $(".xiaoqu").append(str);
        });
    },
    // 获取初始select的值
    getSelect:function(){
        $(".xiaoqu").nextAll().each(function(){
            work.strAll.push($(this).html());
        });
    },
    // 计算数组或对象的长度
    strLength:function(str){
        t=typeof(str);
        if(t=='string'){
            return str.length;
        }else if(t=='object'){
            var n=0;
            for(var i in str){
                n++
            }
            return n;
        }
    },
    // 下拉菜单字符串生成
    // data为数据，id为id索引值
    strAdd:function(data,id,key){
        var str="";
        for (x in data){
            if(data[x]['unit_id']==0){
                console.log('无单元号');
                work.msg('danyuan','单元');
                // break;
            }
            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
        }
        switch(id){
            case 'xiaoqu':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择小区</option>');
                break;
            case 'quyu':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择区域</option>');
                break;
            case 'louhao':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择楼号</option>');
                break;
            case 'danyuan':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择单元</option>');
                break;
            case 'fangjian':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择房间号</option>');
                break;
        }
        $("."+id).append(str);
    },
    // 没有下一级属性时显示的信息
    msg:function(obj,id,msg){
        $(obj).next("."+id).find("option").text('无'+msg);
    },

}